﻿using System.Diagnostics;
using fastdbServer.Middleware;
using Microsoft.AspNetCore.Mvc;

namespace fastdbServer
{
    public class Startup
    {
        public static IServiceProvider ServiceProvider;

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddMvc();
            ServiceProvider = services.BuildServiceProvider().GetService(typeof(IServiceProvider)) as IServiceProvider;
        }



        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            //ASP.NET Core设置默认首页
            //https://blog.csdn.net/windowsliusheng/article/details/112309920
            var defaultFilesOptions = new DefaultFilesOptions();
            defaultFilesOptions.DefaultFileNames.Clear();
            defaultFilesOptions.DefaultFileNames.Add("index.html");
            app.UseDefaultFiles(defaultFilesOptions);

            app.UseStaticFiles();

            // 设置允许所有来源跨域
            app.UseCors("cors");

            app.UseMiddleware(typeof(GlobalErrorHandlerMiddleWare)); //全局错误捕捉

            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    "WebApi",
                    "{area=WebApi}/{controller=Home}/{action=Login}/{id?}");
            });

            Console.WriteLine("系统已启动，地址：http://localhost:" + Program.Port);
        }
    }
}